package com.example.administrator.shzs.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;

import com.example.administrator.shzs.utils.NetWorkUtils;


/**
 * 网络监听类
 */
public class ConnectionChangeReceiver extends BroadcastReceiver {

    private AlertDialog.Builder dialog;

    private NetWorkUtils workUtils;
    private Context context;

    public ConnectionChangeReceiver(Context context) {
        this.context = context;
        workUtils = new NetWorkUtils(context);
    }

    @Override
    public void onReceive(final Context context, Intent intent) {

        dialog = new AlertDialog.Builder(context);
        if (!workUtils.getNetWorkState()) {
            dialog.setMessage("亲，没有网络哦~")
                    .setPositiveButton("打开网络",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    try {

                                        // dialog启动网络管理界面
                                        Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                                        context.startActivity(intent);
                                        dialog.cancel();

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            }).setNegativeButton("取消", null);
            dialog.show();
        }else {

        }

    }
}
